﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.IO;
using FileHelpers;
using System.Data;
using DatabaseInsertAndExportLibrary;
using DatabaseInsertAndExportLibrary.HKEXDatabaseDataSetTableAdapters;


namespace DatabaseInsertAndExportLibrary
{
    class Program
    {
        public static int callputconverter(string callput)
        {


            int cALL_PUT = 0;
            if (callput == "C")
            {
                cALL_PUT = 1;
            }
            else if (callput == "P")
            {
                cALL_PUT = 2;
            }

            return cALL_PUT;



        }
        static void Main(string[] args)
        {
            //CSVAnalyser csvanalyse = new CSVAnalyser(@"C:\temp\hsio120315.csv");
            ////Console.WriteLine(csvanalyse.Analyse(1204, 244));
            ////int startindex = csvanalyse.getstartindex("\"TOP 10 TRADED (BY VOLUME)\",,,\"VOLUME\",\"O.Q.P. CLOSE\",\"IV%\",\"DAILY HIGH\",\"DAILY LOW\",\"OPEN INTEREST\",\"O.Q.P. CHANGE\"") + 4;
            ////int endindex = csvanalyse.getendindex("\"HANG SENG INDEX OPTIONS HK$ 50 PER POINT\"");
            ////Console.WriteLine(csvanalyse.Analyse(startindex, endindex - startindex));
            //Console.WriteLine(csvanalyse.getstring(csvanalyse, "\"TOP 10 TRADED (BY VOLUME)\",,,\"VOLUME\",\"O.Q.P. CLOSE\",\"IV%\",\"DAILY HIGH\",\"DAILY LOW\",\"OPEN INTEREST\",\"O.Q.P. CHANGE\"", "\"HANG SENG INDEX OPTIONS HK$ 50 PER POINT\"", 4));

            // To Read Use:   
            FileHelperEngine engine = new FileHelperEngine(typeof(HsharesIndexFutures1Data));

            HsharesIndexFutures1Data[] hhif = engine.ReadFile(@"C:\temp\hhif120315fixed.csv") as HsharesIndexFutures1Data[];
            foreach (HsharesIndexFutures1Data cust in hhif)
            {

                HsharesIndexFutures1TableAdapter hsharesIndexFutures1TableAdapter = new HsharesIndexFutures1TableAdapter();
                hsharesIndexFutures1TableAdapter.Insert(cust.ContractMonth, cust.OpenPrice, cust.DailyHigh, cust.DailyLow, cust.SettlementPrice, cust.ChangeinSettlementPrice, cust.ContractHigh, cust.ContractLow, cust.Volume, cust.OpenInterest, cust.ChangeinOI);
               
            }


            FileHelperEngine enginehhif = new FileHelperEngine(typeof(HsharesIndexFutures2Data));
            HsharesIndexFutures2Data[] hhif2 = enginehhif.ReadFile(@"C:\temp\hhif120315fixedpart2.csv") as HsharesIndexFutures2Data[];
            foreach (HsharesIndexFutures2Data cust in hhif2)
            {
                HsharesIndexFutures2TableAdapter hsharesIndexFutures2TableAdapter = new HsharesIndexFutures2TableAdapter();
                hsharesIndexFutures2TableAdapter.Insert(cust.ContractMonth.ToString(), cust.null0, cust.Strategy, cust.null1, cust.null2, cust.OpenPrice, cust.DailyHigh, cust.DailyLow, cust.Volume);
               
            }

            FileHelperEngine enginehsif = new FileHelperEngine(typeof(HangSengIndexFutures1Data));

            HangSengIndexFutures1Data[] hsif = enginehsif.ReadFile(@"C:\temp\hsif120315fixed.csv") as HangSengIndexFutures1Data[];
            foreach (HangSengIndexFutures1Data cust in hsif)
            {

                HangSengIndexFutures1TableAdapter hangSengIndexFutures1TableAdapter = new HangSengIndexFutures1TableAdapter();
                hangSengIndexFutures1TableAdapter.Insert(cust.ContractMonth, cust.OpenPrice, cust.DailyHigh, cust.DailyLow, cust.SettlementPrice, cust.ChangeinSettlementPrice, cust.ContractHigh, cust.ContractLow, cust.Volume, cust.OpenInterest, cust.ChangeinOI);
            }

            FileHelperEngine enginehsif2 = new FileHelperEngine(typeof(HangSengIndexFutures2Data));
            HangSengIndexFutures2Data[] hsif2 = enginehsif2.ReadFile(@"C:\temp\hsif120315fixedpart2.csv") as HangSengIndexFutures2Data[];
            foreach (HangSengIndexFutures2Data cust in hsif2)
            {

                HangSengIndexFutures2TableAdapter hangSengIndexFutures2TableAdapter = new HangSengIndexFutures2TableAdapter();
                hangSengIndexFutures2TableAdapter.Insert(cust.ContractMonth.ToString(), cust.null0, cust.Strategy, cust.null1, cust.null2, cust.OpenPrice, cust.DailyHigh, cust.DailyLow, cust.Volume);
            }


            FileHelperEngine enginehsio = new FileHelperEngine(typeof(HangSengIndexOptionsData));
            HangSengIndexOptionsData[] hsio = enginehsio.ReadFile(@"C:\temp\hsio120315fixedpart2.csv") as HangSengIndexOptionsData[];
            foreach (HangSengIndexOptionsData cust in hsio)
            {

                HangSengIndexOptionsTableAdapter hangSengIndexOptionsTableAdapter = new HangSengIndexOptionsTableAdapter();
                hangSengIndexOptionsTableAdapter.Insert(cust.ContractMonth, cust.STRIKEPRICE, callputconverter(cust.CallPut), cust.OPENINGPRICE, cust.DAILYHIGH, cust.DAILYLOW, cust.OQPCLOSE, cust.OQPCHANGE, cust.IV, cust.VOLUME, cust.OPENINGPRICE, cust.CHANGEINOI);
                Console.WriteLine(hangSengIndexOptionsTableAdapter.GetData().ToString());
            }


            FileHelperEngine enginehsiotop = new FileHelperEngine(typeof(HangSengIndexOptionsTop10Data));
            HangSengIndexOptionsTop10Data[] hsiotop = enginehsiotop.ReadFile(@"C:\temp\hsio120315fixed.csv") as HangSengIndexOptionsTop10Data[];
            foreach (HangSengIndexOptionsTop10Data cust in hsiotop)
            {

                HangSengIndexOptionsTop10TableAdapter hangSengIndexOptionsTop10TableAdapter = new HangSengIndexOptionsTop10TableAdapter();
                hangSengIndexOptionsTop10TableAdapter.Insert(callputconverter(cust.CallPut), cust.ContractMonth, cust.STRIKEPRICE, cust.VOLUME, cust.OQPCLOSE, cust.IV, cust.DAILYHIGH, cust.DAILYLOW, cust.OPENINTEREST, cust.OQPCHANGE);

            }

            //FileHelperEngine enginedqename = new FileHelperEngine(typeof(StockOptionsStockname));
            //StockOptionsStockname[] dqename = enginedqename.ReadFile(@"C:\temp\dqe120315.csv") as StockOptionsStockname[];
            //foreach (StockOptionsStockname cust in dqename)
            //{
            //    StockOptionsStocknameTableAdapter stockOptionsStocknameTableAdapter = new StockOptionsStocknameTableAdapter();
            //    HKEXDatabaseDataSet.StockOptionsStocknameDataTable dataTable = stockOptionsStocknameTableAdapter.GetData();
            //    int HKATSCODE = -1;
            //    foreach (var item in dataTable)
            //    {
            //        if (item.HKATSCODE == cust.HKATSCODE)
            //        {
            //            HKATSCODE = item.Id;
            //            break;
            //        }
            //    }
            //    if (HKATSCODE == -1)
            //    {
            //        stockOptionsStocknameTableAdapter.Insert(cust.HKATSCODE, cust.HKATSCODE);
            //        HKATSCODE = dataTable.Count + 1;
            //    }
            //    int UNDERLYINGSTOCK = -1;
            //    foreach (var item in dataTable)
            //    {
            //        if (item.UNDERLYINGSTOCK == cust.UNDERLYINGSTOCK)
            //        {
            //            UNDERLYINGSTOCK = item.Id;
            //            break;
            //        }
            //    }
            //    if (UNDERLYINGSTOCK == -1)
            //    {
            //        stockOptionsStocknameTableAdapter.Insert(cust.UNDERLYINGSTOCK, cust.UNDERLYINGSTOCK);
            //        UNDERLYINGSTOCK = dataTable.Count + 1;
            //    }


            //    stockOptionsStocknameTableAdapter.Insert(cust.HKATSCODE, cust.UNDERLYINGSTOCK);



            //}

            FileHelperEngine enginedqecon = new FileHelperEngine(typeof(StockOptionsContract));

            StockOptionsContract[] dqecon = enginedqecon.ReadFile(@"C:\temp\dqe120315fixedpart2.csv") as StockOptionsContract[];
            foreach (StockOptionsContract cust in dqecon)
            {

                StockOptionsContractTableAdapter stockOptionsContractTableAdapter = new StockOptionsContractTableAdapter();
                stockOptionsContractTableAdapter.Insert(cust.ContractMonth, cust.CLOSINGPRICE, callputconverter(cust.CallPutId), cust.OPENINGPRICE, cust.DAILYHIGH, cust.DAILYLOW, cust.SETTLEMENTPRICE, cust.CHANGEINSETTLEMENT, cust.IV, cust.VOLUME, cust.OPENINTEREST, cust.CHANGEINOI);
            }

            FileHelperEngine enginedqeopt = new FileHelperEngine(typeof(StockOptionsStockOptionData));
            StockOptionsStockOptionData[] dqeopt = enginedqeopt.ReadFile(@"C:\temp\dqe120315fixed.csv") as StockOptionsStockOptionData[];
            foreach (StockOptionsStockOptionData cust in dqeopt)
            {
                StockOptionsStockOptionTableAdapter stockOptionsStockOptionTableAdapter = new StockOptionsStockOptionTableAdapter();
                stockOptionsStockOptionTableAdapter.Insert(cust.HKATSCODE, cust.UNDERLYINGSTOCK, cust.dontknow, cust.TRADINGTOTAL, cust.TRADINGCALLS, cust.TRADINGPUTS, cust.OPENTOTAL, cust.OPENCALLS, cust.OPENPUTS, cust.IV);
            }


            FileHelperEngine enginedqetop10 = new FileHelperEngine(typeof(StockOptionsTop10Data));
            StockOptionsTop10Data[] dqetop10 = enginedqetop10.ReadFile(@"C:\temp\dqe120315fixedpart3.csv") as StockOptionsTop10Data[];
            foreach (StockOptionsTop10Data cust in dqetop10)
            {
                StockOptionsTop10TableAdapter stockOptionsTop10TableAdapter = new StockOptionsTop10TableAdapter();
                stockOptionsTop10TableAdapter.Insert(callputconverter(cust.CallPutId), cust.HKATSCODE, cust.ContractMonth, cust.CLOSINGPRICE, cust.VOLUME, cust.SETTLEMENTPRICE, cust.IV, cust.HIGH, cust.LOW, cust.OPENINTEREST, cust.SETTLEMENTPRICECHANGE, cust.CISPrice);

            }

            FileHelperEngine enginedqetop5d = new FileHelperEngine(typeof(StockOptionsTop5DownData));

            StockOptionsTop5DownData[] dqetop5d = enginedqetop5d.ReadFile(@"C:\temp\dqe120315fixedpart5.csv") as StockOptionsTop5DownData[];
            foreach (StockOptionsTop5DownData cust in dqetop5d)
            {

                StockOptionsTop5DownTableAdapter stockOptionsTop5DownTableAdapter = new StockOptionsTop5DownTableAdapter();
                stockOptionsTop5DownTableAdapter.Insert(callputconverter(cust.CallPutId), cust.HKATSCODE, cust.ContractMonth, cust.CLOSINGPRICE, cust.VOLUME, cust.SETTLEMENTPRICE, cust.IV, cust.HIGH, cust.LOW, cust.OPENINTEREST, cust.SETTLEMENTPRICECHANGE, cust.CISPrice);

            }

            FileHelperEngine enginedqetop5u = new FileHelperEngine(typeof(StockOptionsTop5UpData));
            StockOptionsTop5UpData[] dqetop5u = enginedqetop5u.ReadFile(@"C:\temp\dqe120315fixedpart4.csv") as StockOptionsTop5UpData[];
            foreach (StockOptionsTop5UpData cust in dqetop5u)
            {

                StockOptionsTop5UpTableAdapter stockOptionsTop5UpTableAdapter = new StockOptionsTop5UpTableAdapter();
                stockOptionsTop5UpTableAdapter.Insert(callputconverter(cust.CallPutId), cust.HKATSCODE, cust.ContractMonth, cust.CLOSINGPRICE, cust.VOLUME, cust.SETTLEMENTPRICE, cust.IV, cust.HIGH, cust.LOW, cust.OPENINTEREST, cust.SETTLEMENTPRICECHANGE, cust.CISPrice);

            }




            //Console.ReadLine();

        }
    }

}
